import orm
def show_goods():   #显示商品清单
    sql=f"""
    select num,name,price,discount from goods
"""
    data= orm.read_data(sql)
    if data!=():
        print(f'----编码----名称----价格----折扣----')
        for goods in data:
            print(f"{goods[0]}\t{goods[1]}\t{goods[2]}\t{goods[3]}")
def get_goods():
    while True:
        num=int(input("请输入您要查询的商品编号："))
        sql=f"""
        select * from goods where num={num}
    """
        data= orm.read_data(sql)
        if data!=():
            print(f'----编码----名称----价格----折扣----')
            for goods in data:
                print(f"{goods[0]}\t{goods[1]}\t{goods[2]}\t{goods[3]}")
                break
        else:
            choice=input("商品不存在！继续操作请按“1”，退出请按任意键。。。")
            if choice==1:
                continue
            else:
                break
    return data
def goods_add():
    while True:
        num=int(input("请您创建商品编号："))
        name=input("请输入您要添加的商品名称：")
        price=float(input("请设置商品的价格："))
        discount=float(input("请设置商品的折扣："))
        sql=f"""insert into goods values(null,{num},'{name}',{price},{discount})"""
        rows= orm.write_data(sql)
        print(rows)
        if rows>0:
            print("创建成功！")
            break
        else:
            choice=input("创建失败！继续操作请按“1”，退出请按任意键。。。")
            if choice==1:
                continue
            else:
                break
def goods_update():    #修改商品价格和折扣
    while True:
        num = int(input("请输入您要修改的商品名称："))
        price=float(input("请输入更改后的价格："))
        discount=float(input("请输入更改后的折扣："))
        sql=f"""update goods set price={price} and discount={discount} where num='{num}'"""
        rows= orm.write_data(sql)
        if rows>0:
            print("修改成功！")
            break
        else:     #失败的情况分析
            choice=input("修改失败！继续操作请按“1”，退出请按任意键。。。")
            if choice==1:
                continue
            else:
                break
def goods_del():
      while True:
          num= int(input("请输入您要删除的商品编号："))
          sql=f"""delete from goods where num='{num}'"""
          rows= orm.write_data(sql)
          if rows>0:
              print("商品删除成功！")
              break
          else:   #失败的情况分析
            choice=input("商品编号不存在！继续操作请按“1”，退出请按任意键。。。")
            if choice==1:
                continue
            else:
                break
def show_orders():
    sql=f"""select num,count,amount from orders"""
    data= orm.read_data(sql)
    if data!=():
        print(f"---订单编号----购买数量----总金额----")
        for order in data:
            print(f"{order[1]}\t{order[2]}\t{order[3]}\t")
def del_order():
    while True:
        num=int(input("请输入您要删除的订单编号："))
        sql = f"""delete from orders where num='{num}'"""
        rows= orm.write_data(sql)
        if rows>0:
            print("商品删除成功！")
            break
        else:  # 失败的情况分析
            choice = input("订单不存在！继续操作请按“1”，退出请按任意键。。。")
            if choice == 1:
                continue
            else:
                break
def count_orders():
    sql=f"""select sum(count),sum(amount) from orders"""
    data= orm.read_data(sql)
    if data!=():
        print("----总销量----总销售额----")
        for order in data:
            print(f"\t{order[0]}个\t{order[1]}元")

def add_order():
    amount = 0
    count = 0
    while True:
        data = get_goods()
        if data != ():
            num = int(input('请输入要购买的数量：'))
            money = data[0][2] * num
            count += num
            amount += money
            choice = int(input('请输入你的选择1继续2退出：'))
            if choice == 1:
                continue
            else:
                break
    print(f'总数量{count}总金额{amount}')
    sql = f"""
            select max(num) from orders
        """
    data = orm.read_data(sql)
    id = data[0][0] + 1
    sql = f"""
            insert into orders values(null,{id},{count},{amount})
        """
    rows = orm.write_data(sql)
    if rows > 0:
        print('订单添加成功')
    else:
        print('订单添加失败')

if __name__ == '__main__':
    # show_goods()
    # goods_add()
    # goods_update()
    # goods_del()
    # show_orders()
    # del_order()
    count_orders()
    # add_order()